﻿using System;
using System.Linq;
using System.Collections.Generic;
using Netkk.Common;
using Netkk.Common.Interfaces.Repositories;
using Netkk.Common.Interfaces.Services;
using Netkk.Common.Models;

namespace Netkk.Library.Services
{
    public class UserService : IUserService
    {
        private readonly IUserRepository repository;

        public UserService(IUserRepository repository)
        {
            this.repository = repository;
        }

        //protected bool ValidateUser(User user)
        //{

        //    if (user.Name.Trim().Length == 0)
        //    {
        //        validationDictionary.AddError("Name", "Name is required.");
        //    }

        //    if (String.IsNullOrEmpty(user.Name))
        //    {
        //        validationDictionary.AddError("Name", "Bitte einen Benutzernamen eingeben");
        //    }

        //    if (String.IsNullOrEmpty(user.Password))
        //    {
        //        validationDictionary.AddError("Password", "Bitte ein Passwort eingeben.");
        //    }

        //    if (String.IsNullOrEmpty(user.Email))
        //    {
        //        validationDictionary.AddError("Email", "Bitte eine E-Mail eingeben.");
        //    }

        //    if (!user.Email.IsValidEmail())
        //    {
        //        validationDictionary.AddError("Email", "Bitte eine gültige E-Mail eingeben.");
        //    }

        //    if (GetUserById(user.Id) == null)
        //    {
        //        if (GetUserByLoginName(user.Name) != null)
        //        {
        //            validationDictionary.AddError("Name", "Einen Benutzer mit diesem Benutzername nexistiert bereits.");
        //        }

        //        if (GetUserByEmail(user.Email) != null)
        //        {
        //            validationDictionary.AddError("Email", "Einen Benutzer mit dieser E-Mailadresse existiert bereits.");
        //        }
        //    }

        //    return validationDictionary.IsValid;
        //}

        public IEnumerable<User> ListAllUsers()
        {
            IEnumerable<User> allusers = repository.Select();
            IEnumerable<User> users = from u in allusers
                                      orderby u.SortName ascending
                                      select u;
            return users;
        }

        public IEnumerable<User> ListAllActiveUsers()
        {
            IEnumerable<User> allusers = repository.Select();
            IEnumerable<User> users = from u in allusers
                                      where u.Role.Role != Roles.Guest
                                      && u.AllowShowProfile
                                      && u.IsValid
                                      orderby u.SortName ascending
                                      select u;
            return users;
        }

        public User GetUserById(Guid id)
        {
            IEnumerable<User> allusers = repository.Select();
            User user = (from u in allusers
                         where u.Id == id
                         select u).FirstOrDefault();
            return user;
        }

        public User GetUserByEmail(string email)
        {
            IEnumerable<User> allusers = repository.Select();
            User user = (from u in allusers
                         where u.Email.Equals(email, StringComparison.OrdinalIgnoreCase)
                         select u).FirstOrDefault();
            return user;
        }

        public User GetUserByLogin(string username, string password)
        {
            IEnumerable<User> allusers = repository.Select();
            User user = (from u in allusers
                         where u.Name.Equals(username, StringComparison.OrdinalIgnoreCase)
                           && u.Password == password
                           && u.IsValid
                         select u).FirstOrDefault();
            return user;
        }

        public User GetUserByLoginName(string username)
        {
            IEnumerable<User> allusers = repository.Select();
            User user = (from u in allusers
                         where u.Name.Equals(username, StringComparison.OrdinalIgnoreCase)
                           && u.IsValid
                         select u).FirstOrDefault();
            return user;
        }

        public bool UserIsValid(Guid id)
        {
            User user = GetUserById(id);
            if (user == null || !user.IsValid)
            {
                return false;
            }
            return user.IsValid;
        }

        public void DeleteUser(User user)
        {
            repository.Delete(user);
        }

        public bool SaveUser(User user)
        {
            // Database logic
            try
            {
                repository.Save(user);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public IEnumerable<User> ListAllActiveUsersWithPublicProfile()
        {
            throw new NotImplementedException();
        }

        public IEnumerable<User> ListAllActiveUsersWithNewsletter()
        {
            throw new NotImplementedException();
        }
    }

}
